package com.longer.playhelper.view.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.utils.RegexUtils;
import com.longer.playhelper.Config;
import com.longer.playhelper.R;
import com.longer.playhelper.presenter.LoginPhone_ActivityPresenter;
import com.longer.playhelper.utlis.Toast;
import com.longer.playhelper.view.iview.ILoginPhone_ActivityView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.UpdateListener;

public class LoginPhone_Activity extends AppCompatActivity implements ILoginPhone_ActivityView {

    public Context context;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.edt_login_phone)
    EditText edtLoginPhone;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.edt_login_register)
    TextView edtLoginRegister;
    @Bind(R.id.ll_login_qq)
    LinearLayout llLoginQq;
    public static LoginPhone_Activity instant;
    @Bind(R.id.edt_login_phone_yzm)
    EditText edtLoginPhoneYzm;
    @Bind(R.id.rl_loginphone)
    RelativeLayout rlLoginphone;
    ProgressDialog dialog;
    ProgressDialog progressDialog;

    private LoginPhone_ActivityPresenter activityPresenter = new LoginPhone_ActivityPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_phone);
        ButterKnife.bind(this);
        toolbar.setTitle("手机登录");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        inti();
    }

    public void inti() {
        context = LoginPhone_Activity.this;
        instant = LoginPhone_Activity.this;
        activityPresenter.checkLogin();

        dialog = new ProgressDialog(context);
        dialog.setCancelable(false);
        dialog.setMessage("正在登录中...");

        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在发送验证码");

    }


    @Override
    public void showSendSMSdialogTip(String phone) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("Tip");
        dialog.setCancelable(true);
        dialog.setMessage("发送验证码到：\r\n" + phone + "\r\n请确认手机号无误？");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activityPresenter.SendSMSNoInfor();
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
    }

    @Override
    public void showSendSMSdialog() {
        progressDialog.show();
    }

    @Override
    public void hideSendSMSdialog() {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void sendSMSSuccess() {
        Toast.show("短信发送成功");
    }

    @Override
    public void sendSMSFailed(String e) {
        Toast.show("短信发送失败:" + e);
    }

    @Override
    public void setBtnText(String str) {
        btnLogin.setText(str);
    }

    @Override
    public void setPhone(String phone) {
        edtLoginPhone.setText(phone);
    }

    @Override
    public void setPhoneEnable(boolean enable) {
        if (enable) {
            edtLoginPhone.setEnabled(true);
        } else {
            edtLoginPhone.setEnabled(false);
        }
    }

    @Override
    public void setYzmVisibale(boolean isshow) {
        if (isshow) {
            rlLoginphone.setVisibility(View.VISIBLE);
        } else {
            rlLoginphone.setVisibility(View.GONE);
        }
    }

    @Override
    public String getBtnText() {
        return btnLogin.getText().toString();
    }

    @Override
    public String getPhone() {
        return edtLoginPhone.getText().toString().trim();
    }

    @Override
    public String getYzm() {
        return edtLoginPhoneYzm.getText().toString().trim();
    }

    @Override
    public void showLoginDialog() {
        dialog.show();
    }

    @Override
    public void hideLoginDialog() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void loginSuccess() {
        Toast.show("登录成功");
    }

    @Override
    public void loginFailed(String e) {
        Toast.show("登录失败:" + e);
    }

    @Override
    public void toMain_Activity() {
        this.finish();
    }

    @Override
    public void showRegisterDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("提示");
        builder.setMessage("发现改手机号尚未注册，是否前往注册");
        builder.setPositiveButton("注册", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                toRegister_Activity();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    @Override
    public void toRegister_Activity() {
        Intent intent = new Intent(context, Register_Activity.class);
        intent.putExtra("phone", getPhone());
        intent.putExtra("yzm", getYzm());
        startActivity(intent);
    }

    @OnClick({R.id.btn_login, R.id.edt_login_register, R.id.ll_login_qq, R.id.btn_login_phone_yzm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if ("登录".equals(getBtnText())) {
                    if (!RegexUtils.isMobileExact(getPhone())) {
                        edtLoginPhone.setError("手机号格式不正确");
                        break;
                    }
                    if (getYzm().isEmpty()) {
                        Toast.show("验证码不能为空！");
                        break;
                    }
                    activityPresenter.login();
                } else if ("注销".equals(getBtnText())) {
                    activityPresenter.logout();
                }
                break;
            case R.id.edt_login_register:
                startActivity(new Intent(context, Register_Activity.class));
                break;
            case R.id.ll_login_qq:
                Toast.show("建设中...");
                break;
            case R.id.btn_login_phone_yzm:
                if (!RegexUtils.isMobileExact(getPhone())) {
                    edtLoginPhone.setError("手机号格式不正确");
                    break;
                }
                showSendSMSdialogTip(getPhone());
                break;
        }
    }


}
